<!-- 商机管理 -->
<template>
  <div class="nicheManage">
    <el-card>
      <div class="opt_wrap">
        <div class="opt_wrap_left">
          <p>
            已选中<span style="color: #1796EB">{{ selectedNum }}</span
            >项
          </p>
          <!-- <p v-if="showBtn" @click="closeNiche">
            <i class="iconfont icon-cuo"></i>
            未成单关闭
          </p> -->
          <!-- <p v-if="showBtn">
            <i class="iconfont icon-fenpai"></i>
            分派
          </p>
          <p v-if="showBtn" @click="showDelModal = true">
            <i class="iconfont icon-gongxiang"></i>
            分享
          </p> -->
          <p v-if="showBtn" @click="showDelModal = true">
            <i class="iconfont icon-shanchu1"></i>
            删除
          </p>
        </div>
        <div class="opt_wrap_right">
          <p>
            <i class="iconfont icon-daochu"></i>
            导出
          </p>
          <!-- <p class="add" @click="goToURL('nicheDetail', {pageType: 'create'})">
            <i class="iconfont icon-xinjian1" style="color: white; font-size: 12px; margin-right: 4px;"></i>
            新建
          </p> -->
        </div>
      </div>
      <statisticsTag
        v-if="statisticsTagData"
        :data="statisticsTagData"
        @searchStatus="searchStatus"
      />
      <div class="search_wrap">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-input
              placeholder="商机主题/潜客名称"
              v-model="searchCondition"
              clearable
              style="width: 260px;"
              @keyup.enter.native="queryListData"
            >
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
          </el-col>
          <el-col :span="8">
            <el-date-picker
              v-model="queryDate"
              @change="queryListData"
              type="daterange"
              range-separator="~"
              start-placeholder="注册日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-col>
          <el-col :span="5">
            <el-select
              v-model="opport_status"
              placeholder="商机进度"
              @change="queryListData"
              filterable
              clearable
            >
              <el-option
                v-for="item in statisticsTagData"
                :key="item.label"
                :label="item.label"
                :value="item.status"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="5">
            <el-select
              v-model="type"
              placeholder="商机类型"
              @change="queryListData"
              filterable
              clearable
            >
              <el-option
                v-for="item in nicheType"
                :key="item.id"
                :label="item.type_name"
                :value="item.id"
              >
                <!-- {{ item.type_name }} -->
              </el-option>
            </el-select>
          </el-col>
        </el-row>
      </div>
      <div class="table_list">
        <el-table
          ref="nicheTable"
          :data="tableListData"
          min-height="500"
          border
          v-loading="loading"
          @selection-change="handleSelectionNiche"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="40" />
          <el-table-column
            v-for="(item, index) in tableCaption"
            :key="index"
            :label="item.label"
            align="center"
          >
            <template slot-scope="scope">
              <span v-if="item.value !== 'name'">{{
                scope.row[item.value]
              }}</span>
              <span
                v-if="item.value === 'name'"
                @click="
                  goToURL('nicheDetail', {
                    pageType: 'details',
                    id: scope.row.id
                  })
                "
                class="see_detail"
                >{{ scope.row[item.value] }}</span
              >
            </template>
          </el-table-column>
          <el-table-column label="来源" align="center">
            <template slot-scope="scope">
              <div
                v-if="scope.row.source_arr"
                class="flag"
                :style="{ background: scope.row.source_arr.c }"
              >
                {{ scope.row.source_arr.a }}
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column
            label="商机状态"
            align="center">
            <template slot-scope="scope">
              <p>{{ scope.row.state }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="商机类型"
            align="center">
            <template slot-scope="scope">
              <p>{{ scope.row.type }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="负责人"
            align="center">
            <template slot-scope="scope">
              <p>{{ scope.row.username }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center">
            <template slot-scope="scope">
              <p>{{ scope.row.create_time }}</p>
            </template>
          </el-table-column> -->
          <el-table-column
            v-for="item in tableCaption2"
            :key="item.label"
            :label="item.label"
            align="center"
          >
            <template slot-scope="scope">
              <span v-if="item.value !== 'name'">{{
                scope.row[item.value]
              }}</span>
              <span
                v-if="item.value === 'name'"
                @click="goToURL('nicheDetail', { type: 'see' })"
                class="see_detail"
                >{{ scope.row[item.value] }}</span
              >
            </template>
          </el-table-column>
        </el-table>
        <link-pagination
          :total="total"
          :currentPage="currentPage"
          :limit="limit"
          v-if="total != 0"
          @pagination="getPaginationParams"
        ></link-pagination>
      </div>
    </el-card>
    <!-- 未成单关闭弹框 -->
    <el-dialog title="未成单关闭" width="510px" :visible.sync="closeModal">
      <div class="closeModalContent" style="position: relative">
        <p style="margin-bottom: 20px;">关闭原因</p>
        <el-input type="textarea" :rows="5" v-model="closeReason"> </el-input>
        <p
          v-if="noReason"
          style="position: absolute;left: 0;bottom: -23px; color: red;"
        >
          请填写关闭原因
        </p>
      </div>
      <div class="btn">
        <el-button @click="closeModal = false">返回</el-button>
        <el-button type="primary" @click="confirmClose">确认</el-button>
      </div>
    </el-dialog>
    <!-- 删除弹框 -->
    <el-dialog
      title="提示"
      width="510px"
      :visible.sync="showDelModal"
      class="del_modal"
    >
      <div class="content">
        <p><i class="iconfont icon-tishi"></i></p>
        <!-- <p v-if="selectionCustomer.length === 1">是否将客户<span>{{ selectionCustomer[0].name }}删除？</span></p>
        <p v-if="selectionCustomer.length > 1">是否将<span>{{ selectionCustomer[0].name }}、{{ selectionCustomer[1].name }}<span v-if="selectionCustomer.length > 2">等</span>{{ selectionCustomer.length }}位客户删除？</span></p> -->
        <p>删除成功之后，该操作将无法恢复</p>
      </div>
      <div class="btn">
        <el-button @click="showDelModal = false">返回</el-button>
        <el-button type="primary" @click="confirmDelete">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import statisticsTag from "@/components/statisticsTag";
import { paginationMixin } from "@/mixin";
import { formatDate, formatArray } from "@/utils";
import http from "@/api/niche";
import contractApi from "@/api/contract";
export default {
  name: "nicheManage",
  mixins: [paginationMixin],
  data() {
    return {
      selectedNum: 0,
      showBtn: false,
      searchCondition: null,
      queryDate: null,
      step: null,
      steps: null,
      type: null,
      loading: false,
      closeModal: false,
      showDelModal: false,
      closeReason: "",
      noReason: false,
      total: 1,
      delNiche: [],
      contract_status: [], // 商机状态
      formData: {}, // 未成单关闭表单
      tableCaption: [
        {
          label: "商机主题",
          value: "title"
        },
        {
          label: "潜客姓名",
          value: "name"
        },
        {
          label: "联系电话",
          value: "mobile"
        },
        {
          label: "商机编号",
          value: "opportNo"
        },
        {
          label: "状态描述",
          value: "opport_status"
        }
      ],
      tableCaption2: [
        {
          label: "商机编号",
          value: "opportNo"
        },
        {
          label: "商机类型",
          value: "type_name"
        },
        {
          label: "负责人",
          value: "username"
        },
        {
          label: "创建时间",
          value: "create_time"
        }
      ],
      tableListData: [
        {
          theme: "新年活动",
          name: "锅锅",
          phone: "18368546116",
          statusDec: "正在进行",
          source: "小程序",
          niche_status: "已跟进",
          niche_type: "成人商机",
          charge: "张三",
          create_time: "2019-10-10"
        }
      ],
      statisticsTagData: [],
      nicheType: null,
      opport_status: null,
      nicheStatus: null,
      backgroundStart: [
        "#17B5EB",
        "#F27450",
        "#ffc31f",
        "#a946eb",
        "#02c779",
        "#17B5EB",
        "#F27450",
        "#ffc31f",
        "#a946eb",
        "#02c779"
      ],
      backgroundEnd: [
        "#1797EB",
        "#F25751",
        "#ffa41e",
        "#9346eb",
        "#02c765",
        "#1797EB",
        "#F25751",
        "#ffa41e",
        "#9346eb",
        "#02c765"
      ]
    };
  },
  components: {
    statisticsTag
  },
  methods: {
    searchStatus(status) {
      this.opport_status = status;
      this.getListData();
    },
    goToURL(name, params) {
      this.$router.push({ name, params });
    },
    queryListData() {
      this.getListData();
    },
    getListData() {},
    handleSelectionNiche(val) {
      this.selectionCustomer = val;
      this.selectedNum = this.selectionCustomer.length;
      if (this.selectedNum === 0) {
        this.showBtn = false;
      } else {
        this.showBtn = true;
      }
      if (this.selectionCustomer.length !== 0) {
        this.delNiche = [];
        for (let item of this.selectionCustomer) {
          this.delNiche.push(item.id);
        }
      }
    },
    /* // 未成单关闭modal
    closeNiche() {
      this.closeModal = true;
      this.noReason = false;
    }, */
    // 确认关闭
    confirmClose() {
      if (this.closeReason === "") {
        this.noReason = true;
      } else {
        this.closeModal = false;
      }
    },
    // 确认删除
    confirmDelete() {
      let id = this.delNiche;
      http.deleteContent({ id }).then(res => {
        this.$message.success(res.msg || "删除成功！");
        this.showDelModal = false;
        this.getListData();
      });
    },
    getListData(
      page = this.currentPage,
      list_rows = this.limit,
      search = this.searchCondition,
      opport_status = this.opport_status
    ) {
      this.loading = true;
      let params = { page, list_rows, search, opport_status };
      if (this.queryDate) {
        params["create_time_start"] = formatDate.toStr(
          new Date(this.queryDate[0]),
          "YYYY-MM-DD"
        );
        params["create_time_end"] = formatDate.toStr(
          new Date(this.queryDate[1]),
          "YYYY-MM-DD"
        );
      }
      let basicConfig = JSON.parse(sessionStorage.getItem("basicConfig"));
      this.nicheStatus = formatArray(basicConfig.opport_status);
      console.log(this.nicheStatus);
      http.getList(params).then(res => {
        this.loading = false;
        this.total = res.data.total;
        let tableListData = res.data.data;
        if (tableListData.length) {
          tableListData.map(item => {
            for (let niche of this.nicheStatus) {
              if (item.opport_status === niche.label.status) {
                item.opport_status = niche.label.label;
                console.log(niche);
              }
            }
            item.birthday = formatDate.toStr(
              new Date(item.birthday),
              "YYYY-MM-DD"
            );
            item.sex = item.sex === 1 ? "男" : "女";
          });
        }
        this.tableListData = tableListData;
      });
    }
  },
  created() {
    this.getListData();
    http.getContractStatus().then(res => {
      this.statisticsTagData = res.data;
      this.statisticsTagData.map((item, index) => {
        item.icon = "icon-weifukuan-";
        item.backgroundStart = this.backgroundStart[index];
        item.backgroundEnd = this.backgroundEnd[index];
      });
    });
    contractApi.getContractType().then(res => {
      this.nicheType = res.data;
    });
  }
};
</script>

<style lang="scss" scoped>
.nicheManage {
  padding: 10px;
  .opt_wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 34px;
    &_left {
      display: flex;
      p {
        margin-right: 18px;
        color: #565656;
        font-size: 16px;
        cursor: pointer;
        &:hover {
          color: #1796eb;
        }
        &:nth-of-type(1) {
          cursor: default;
          color: #646464;
          &:hover {
            color: #646464;
          }
        }
      }
    }
    &_right {
      display: flex;
      align-items: center;
      p {
        margin-left: 18px;
        color: #565656;
        font-size: 16px;
        cursor: pointer;
        &:hover {
          color: #1796eb;
        }
      }
      .add {
        width: 72px;
        height: 30px;
        background: linear-gradient(top left, #17b5eb, #1796eb);
        display: flex;
        justify-content: center;
        align-items: center;
        color: white;
        cursor: pointer;
        border-radius: 5px;
        &:hover {
          color: white;
        }
      }
    }
  }
  .search_wrap {
    margin: 34px 0;
    div {
      //   margin-right: 17px;
    }
  }
  .see_detail {
    cursor: pointer;
    color: #1796eb;
  }
  .btn {
    margin-top: 30px;
    width: 100%;
    display: flex;
    justify-content: center;
    /deep/ .el-button {
      width: 214px;
    }
  }
  .flag {
    padding: 0px 5px;
    background: #c6c7c9;
    color: #fff;
    border-radius: 14px;
  }
  .del_modal {
    .content {
      display: flex;
      flex-direction: column;
      align-items: center;
      p {
        &:nth-of-type(1) {
          width: 92px;
          height: 92px;
          border-radius: 50%;
          background: linear-gradient(to bottom, #f25851, #f27350);
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 18px;
          i {
            font-size: 40px;
            color: white;
          }
        }
        &:nth-of-type(2) {
          font-size: 20px;
          color: #333;
        }
        &:nth-of-type(3) {
          font-size: 14px;
          color: #999;
          margin-top: 7px;
        }
      }
    }
  }
}
</style>
